(function (exports) {
    var $ = null;

    var ActivityLayer = cc.Layer.extend({
        oldItem: {}, //监测小红点
        ActivityData: [],
        onEnter: function () {
            var that = this;
            cc.Layer.prototype.onEnter.call(this);
            var url = 'http://activity.yygameapi.com:21011/activityCheck?appid=' + app.area;
            http.get(url, function (data) {
                data = JSON.parse(data);
                // cc.log("data=="+JSON.stringify(data));
                if(data.data){
                    eventManager.dispatchEvent(EventKey.UpdateActivityMessageNum,true);
                    that.ActivityData = data.data;
                    that.setRedPointDate(); //小红点机制
                    that.initBtnList(1);
                }

            }, function () {
                showAlert("获取失败!");
            });
        },
        setRedPointDate: function () {
            var that = this;
            //对比本地是否展示小红点
            that.oldItem = cc.sys.localStorage.getItem('ActivityShow');
            if (that.oldItem) {
                that.oldItem = JSON.parse(that.oldItem);
            } else {
                that.oldItem = {};
                for (var i = 0; i < that.ActivityData.length; i++) {
                    var aData = that.ActivityData[i];
                    that.oldItem[aData.id + ""] = 1;
                }
            }

            for (var i = 0; i < that.ActivityData.length; i++) {
                var aData = that.ActivityData[i];
                if (!that.oldItem[aData.id + ""]) {
                    that.oldItem[aData.id + ""] = 1;
                }
            }
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
            cc.sys.localStorage.setItem('ActivityShow', JSON.stringify(this.oldItem));
        },
        ctor: function () {
            this._super();
            var that = this;

            var scene = loadNodeCCS(res.ActivityLayer_json, this);
            $ = create$(this.getChildByName('Layer'));
            this._$ = $;


            this._$('pannelBg.txtEmpty').setVisible(false);
            TouchUtil.setOnclickListener(this._$('pannelBg.btnClose'), function (node) {
                that.getParent().removeFromParent(true);
            });
            this._$('pannelBg.gg').setVisible(false);
            TouchUtil.setOnclickListener(this._$('pannelBg.btn_activity'), function (node) {
                that._$('pannelBg.gg').setVisible(false);
                that._$('pannelBg.hd').setVisible(true);
                that.initBtnList(1);
            });

            TouchUtil.setOnclickListener(this._$('pannelBg.btn_gonggao'), function (node) {
                that._$('pannelBg.gg').setVisible(true);
                that._$('pannelBg.hd').setVisible(false);
                that.initBtnList(2);
            });
            that._listView_Y = this._$('pannelBg.btnList').getPositionY();
        },
        initBtnList: function (_type) {
            var that = this;

            var arr = [];
            for (var i = 0; i < that.ActivityData.length; i++) {
                if (that.ActivityData[i].type == _type) {
                    arr.push(that.ActivityData[i]);
                }
            }

            this._$('pannelBg.txtEmpty').setVisible(arr.length == 0);
            var listView = this._$('pannelBg.btnList');
            listView.removeAllChildren();
            var length = arr.length;
            listView.setContentSize(cc.size(listView.getContentSize().width, 110 * length< 550?550:110 * length))
            if (110 * length < 550) {
                var dis = 550 - 110 * length;
                // listView.setPositionY(that._listView_Y + dis);
            }else{
                // listView.setPositionY(that._listView_Y);
            }
            for (var i = 0; i < arr.length; i++) {
                (function (i) {
                    var data = arr[i];
                    var custom_item = new ccui.Layout();
                    custom_item.setContentSize(cc.size(285, 110));
                    custom_item.setName("Layout");
                    listView.pushBackCustomItem(custom_item); //添加cell
                    //创建node
                    var node = duplicateNode(that._$('pannelBg.btnSel'));
                    node.setAnchorPoint(0, 0);
                    node.setPosition(0, 0);
                    node.setName('node');
                    custom_item.addChild(node, 5);
                    node.getChildByName('txtTitle').setString(data.name); //设置活动名称

                    if (i == 0) {
                        node.setTexture(that._$('pannelBg.btnSel2').getTexture());
                        that.initContent(arr, 0);//默认显示第一个
                        that.oldItem[data.id + ""] = 2;
                    }
                    //没有保存改id 或者 值为0 则表示新加 或未查看
                    if (that.oldItem[data.id + ""] == 1) {
                        node.getChildByName('imgRedDot').setVisible(true); //显示小红点
                    }

                    var TouchIdx = i;
                    TouchUtil.setOntouchListener(node, undefined, {
                        onTouchBegan: function (node, touch) {
                            node.startPos = node.convertToWorldSpace(cc.p(0, 0));
                        },
                        onTouchEnded: function (node, event) {
                            var endPos = node.convertToWorldSpace(cc.p(0, 0));
                            if (Math.abs(endPos.y - node.startPos.y) <= 50) {
                                //去掉小红点
                                node.getChildByName('imgRedDot').setVisible(false); //显示小红点
                                that.oldItem[data.id + ""] = 2;
                                //切换状态
                                that.selectBtn();
                                node.setTexture(that._$('pannelBg.btnSel2').getTexture());
                                that.initContent(arr, TouchIdx);
                            }
                        },
                        swallowTouches: false
                    });
                })(i);
            }
        },
        selectBtn: function () {
            var allChild = this._$('pannelBg.btnList').getChildren();
            for (var i = 0; i < allChild.length; i++) {
                allChild[i].getChildByName('node').setTexture($('pannelBg.btnSel').getTexture());
            }
        },
        initContent: function (arr, idx) {
            var that = this;
            var data = arr[idx];
            cc.textureCache.addImageAsync(data.image, function (texture) {
                that._$('pannelBg.panelContent').removeAllChildren();
                var size = that._$('pannelBg.panelContent').getContentSize();
                var sprite = new cc.Sprite(texture);
                sprite.setAnchorPoint(0.5, 0.5);
                sprite.setPosition(size.width / 2, size.height / 2);
                that._$('pannelBg.panelContent').addChild(sprite);

                //点击图片跳转 参数 data.url    "friends" 跳转亲友圈    "play"跳转金币场
                TouchUtil.setOnclickListener(sprite, function (node) {
                    if (data.url && data.url.length > 0) {
                        if (data.url == "friends") { //跳转到亲友圈
                            that.getParent().addChild(that.addChild(new ClubMainLayer()));
                        } else if (data.url == "play") {//跳转到金币场

                        }
                    }
                });

            }, null);
        },


    });
    exports.ActivityLayer = ActivityLayer;

})(window);